*******************************************************************************
* Purpose: Create fertility and cohabitation outcomes
* only commented on in paper appendix
*******************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

use "$raw\MFR2014", clear
forvalues i =2015/2017 {
append using "$raw\MFR`i'"
}
// Define yob
	rename foedselsaar yob
	
keep if yob>=2012

drop if DOEDSDATO_BARN==foedselsdato

keep CPR_BARN CPR_MO foedselsdato
rename CPR_BARN pnr
rename CPR_MODER cprm
rename foedselsdato dob
drop if pnr == ""
sort pnr dob
by pnr: keep if _n == 1
save "$work\children2017", replace

use "$raw\GRUNDVIVE2018", clear
append using "$raw\GRUNDVIVE2019"
append using "$raw\GRUNDVIVE2020"
append using "$raw\GRUNDVIVE2021"
keep pnr MOR_ID FOED_DAG
gen y = year(FOED_DAG)
keep if y == 2018 | y == 2019 | y == 2020 
drop y
rename MOR_ID cprm
rename FOED_DAG dob
drop if pnr == ""
sort pnr dob
by pnr: keep if _n == 1
save "$work\children2020", replace

use "$work\children2017",clear
append using "$work\children2020"
drop if cprm == ""
sort cprm dob
by cprm dob: keep if _n == 1
by cprm: gen b_order = _n
rename dob dobb
drop pnr
reshape wide dobb, i(cprm) j(b_order)

rename cprm id_mother
merge 1:m id_mother using "$work\mfr_kids"
keep if _merge==3
drop _merge

// Create variables for sibling born in years 1-5 after focal child
drop mfr_yob id_father mfr_muni
gen fert1 = 0
gen fert2 = 0
gen fert3 = 0
gen fert4 = 0
gen fert5 = 0
gen nokids1 = 0
forvalues i = 1/6 {
replace fert1 = 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*1
replace fert2 = 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*2
replace fert3 = 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*3
replace fert4 = 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*4
replace fert5 = 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*5
}
forvalues i = 1/6 {
replace nokids1 = nokids1 + 1 if dobb`i' - mfr_dob > 2 & dobb`i' - mfr_dob < 365.3*5
}
keep pnr fert* nokids
save "$work\fertility", replace


* cohabitation
forvalues i = 2012/2022 { // get family id for mothers and father in outcome period. If fam id equal -> parents cohabit
use "$raw\GRUNDVIVE`i'",clear 
gen year = `i'
drop MOR_ID
duplicates drop
rename pnr id_mother
keep id_mother FAMILIE_ID
foreach var in FAMILIE_ID {
rename `var' `var'_m    
}
duplicates drop 
save "$work\cohabm_`i'", replace
}
forvalues i = 2012/2022 {
use "$raw\GRUNDVIVE`i'" ,clear
gen year = `i'
drop FAR_ID
duplicates drop
rename pnr id_father
keep id_father FAMILIE_ID
foreach var in FAMILIE_ID {
rename `var' `var'_f    
}
duplicates drop 
save "$work\cohabf_`i'", replace
}

forvalues i = 2012/2017 { // kids born btw 2012-2017. merge family ids from birth to +5y
forvalues j = 0/5 {
use "$work\mfr_kids", clear
keep if mfr_yob == `i'
local f = `i' + `j' // j number of years after birth
keep pnr id_father id_mother mfr_yob 
merge m:1 id_mother using "$work\cohabm_`f'", keep(1 3) nogen
drop if pnr == ""

merge m:1 id_father using "$work\cohabf_`f'", keep(1 3) nogen
drop if pnr == ""
keep pnr mfr_yob id_mother id_father FAMILIE_ID_m FAMILIE_ID_f
save "$work\divorce`i'_`j'", replace
}
}

// Create indicator for mom and dad cohabitating y 1-5 based on family id of mother and father
forvalues j = 0/5 {
use "$work\divorce2012_`j'", clear
forvalues i = 2013/2017 {
append using "$work\divorce`i'_`j'", force    
}
gen cohab`j' = 0
replace cohab`j' = 1 if FAMILIE_ID_m == FAMILIE_ID_f
replace cohab`j' = 0 if FAMILIE_ID_m == ""
keep pnr cohab`j'
save "$work\cohab`j'", replace
}

use "$work\cohab0", clear
forvalues j =1/5 {
merge 1:1 pnr using "$work\cohab`j'", keep(1 3) nogen
}
save "$work\cohab", replace
